﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using S3.Biz;
using S3.Common;
using System.Globalization;

namespace S3
{
    public partial class Reports : S3FormBase
    {
        public Reports()
        {
            InitializeComponent();
            InitData();

            LoadDailyData();
        }

        void InitData()
        {                                             
            //dtpDailyCalendar.Format = DateTimePickerFormat.Custom;
            //dtpDailyCalendar.CustomFormat = Application.CurrentCulture.DateTimeFormat.ShortDatePattern;

            dtpDailyCalendar.DateTime = DateTime.Now;
            comboMonth.SelectedIndex = DateTime.Now.Month - 1;

            comboYear.Items.Add(DateTime.Now.Year - 5);
            comboYear.Items.Add(DateTime.Now.Year - 4);
            comboYear.Items.Add(DateTime.Now.Year - 3);
            comboYear.Items.Add(DateTime.Now.Year - 2);
            comboYear.Items.Add(DateTime.Now.Year - 1);
            comboYear.Items.Add(DateTime.Now.Year);
            comboYear.SelectedItem = DateTime.Now.Year;
        }

        private void LoadDailyData()
        {
            #region Daily
            DateTime selectedDate = dtpDailyCalendar.DateTime;
            decimal totalDailyGameMoney = 0;
            decimal totalDailyMoney = 0;
            using (DailyTimingEntryManager manager = new DailyTimingEntryManager())
            {
                var dailyGameMoneyList = manager.ReportGameMoneyResult(selectedDate, ReportType.Daily, ref totalDailyGameMoney, ref totalDailyMoney);
                gridDailyGames.DataSource = dailyGameMoneyList;

                lblGameMoney.Text = Utils.StringFormatMoney(totalDailyGameMoney);
            }

            decimal totalServiceMoney = 0;
            using (DailyItemEntryManager manager = new DailyItemEntryManager())
            {
                var dailyServiceMoneyList = manager.ReportServiceMoneyResult(selectedDate, ReportType.Daily, ref totalServiceMoney);
                gridDailyServices.DataSource = dailyServiceMoneyList;

                lblServiceMoney.Text = Utils.StringFormatMoney(totalServiceMoney);
            }

            lblTotalDailyMoney.Text = Utils.StringFormatMoney(totalDailyMoney);
            #endregion

        }

        void LoadMonthlyData()
        {
            #region Monthly
            decimal totalMonthlyGameMoney = 0;
            decimal totalMonthlyMoney = 0;
            int month = comboMonth.SelectedItem == null ? DateTime.Now.Month : Convert.ToInt32(comboMonth.SelectedItem);
            int year = comboYear.SelectedItem == null ? DateTime.Now.Year : Convert.ToInt32(comboYear.SelectedItem);

            DateTime selectedMonth = new DateTime(year, month, 1);

            using (DailyTimingEntryManager manager = new DailyTimingEntryManager())
            {
                var monthlyGameMoneyList = manager.ReportGameMoneyResult(selectedMonth, ReportType.Monthly, ref totalMonthlyGameMoney, ref totalMonthlyMoney);

                gridMonthlyGameMoney.DataSource = monthlyGameMoneyList;

                lblTotalMonthlyGameMoney.Text = Utils.StringFormatMoney(totalMonthlyGameMoney);
            }

            decimal totalMonthlyServiceMoney = 0;

            using (DailyItemEntryManager manager = new DailyItemEntryManager())
            {
                var monthlyServiceMoneyList = manager.ReportServiceMoneyResult(selectedMonth, ReportType.Monthly, ref totalMonthlyServiceMoney);

                gridMonthlyServiceMoney.DataSource = monthlyServiceMoneyList;

                lblTotalMonthlyServiceMoney.Text = Utils.StringFormatMoney(totalMonthlyServiceMoney);
            }

            lblTotalMonthlyMoney.Text = Utils.StringFormatMoney(totalMonthlyMoney);
            #endregion
        }


        private void comboMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadMonthlyData();
        }

        private void comboYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadMonthlyData();
        }

        #region ProcessCmdKey

        /// <summary>
        /// Close form if click ESC
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        
        protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
        {
            try
            {
                if (msg.WParam.ToInt32() == (int)Keys.Escape)
                {
                    this.Close();
                }
                else
                {
                    return base.ProcessCmdKey(ref msg, keyData);
                }
            }
            catch (Exception Ex)
            {
                XtraMessageBox.Show("Key Overrided Events Error:" + Ex.Message);
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }
        #endregion

        private void dtpDailyCalendar_ValueChanged(object sender, EventArgs e)
        {
            LoadDailyData();
        }

        private void xtraTabControl_TabIndexChanged(object sender, EventArgs e)
        {
            switch (xtraTabControl.SelectedTabPageIndex)
            {
                case 1://Montly
                    LoadMonthlyData();
                    break;
                default://Daily
                    LoadDailyData();
                    break;
            }
        }

    }
}